﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Client;
using Flower.Directory.Host.Default;
using Flower.Testing;
using NUnit.Framework;
using Flower.Workflow;
using System.Runtime.Serialization;

#pragma warning disable 0649

namespace Flower.Processing.Tests
{
    [TestFixture]
    public class ProcessorTests
    {
        private interface ITestService
        {
            void Method();
        }

        private class TestService : ITestService
        {
            public void Method()
            {
            }
        }

        [DataContract]
        private class ServiceInjectionProcess : Process, IProcessDefinition
        {
            [Service("TestServices/TestService")]
            private ITestService _privateServiceField;

            public bool IsPrivateServiceFieldSet { get { return _privateServiceField != null; } }

            [Service("TestServices/TestService")]
            public ITestService _publicServiceField;

            [Service("/Services/TestServices/TestService")]
            private ITestService PrivateServiceProperty { get; set; }

            public bool IsPrivateServicePropertySet { get { return PrivateServiceProperty != null; } }

            [Service("/Services/TestServices/TestService")]
            public ITestService PublicServiceProperty { get; set; }

            public void DefineLocalSets(ISetsBuilder bld)
            {
            }

            public void DefineProcess(IProcessBuilder process)
            {
            }
        }

        [Test]
        public void ServicesInjection()
        {
            string servicesConfig =
                @"<objects xmlns=""http://www.springframework.net"">" +
                @"  <object id=""TestService"" type=""" + typeof(TestService).AssemblyQualifiedName + @""" />" +
                @"</objects>";

            var dir = new MemoryDirectory.Directory(new ScriptEnginePool(1));
            new DirectoryBuilder(dir)
                .Root("/Services")
                    .ServiceContainer("TestServices", servicesConfig).End()
                .End();

            var proc = new Processor("Processor", new FlowerClient(dir));
            var prc = new ServiceInjectionProcess();

            proc.Init("PID", prc);

            Assert.IsTrue(prc.IsPrivateServiceFieldSet);
            Assert.IsTrue(prc.IsPrivateServicePropertySet);
            Assert.IsTrue(prc.PublicServiceProperty != null);
            Assert.IsTrue(prc._publicServiceField != null);
        }
    }
}
